import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'
import path from 'path'
import { resolve, dirname } from 'node:path'
import vueI18n from '@intlify/vite-plugin-vue-i18n'
import { fileURLToPath } from 'url'

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        AutoImport({
            // targets to transform
            include: [/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
                /\.vue$/, /\.vue\?vue/, // .vue
                /\.md$/, // .md
            ], // global imports to register
            imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
            resolvers: [
                ElementPlusResolver(), // 自动导入图标组件
            ],

            // Filepath to generate corresponding .d.ts file.
            // Defaults to './auto-imports.d.ts' when `typescript` is installed locally.
            // Set `false` to disable.
            dts: './auto-imports.d.ts',
        }),
        Components({
            resolvers: [
                ElementPlusResolver(), // 自动注册图标组件
            ],
        }),
        createSvgIconsPlugin({
            // 指定要缓存的文件夹
            iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')], // 指定symbolId格式
            symbolId: 'icon-[name]'
        }),
        vueI18n({
            // if you want to use Vue I18n Legacy API, you need to set `compositionOnly: false`
            compositionOnly: true,
            // you need to set i18n resource including paths !
            include: resolve(dirname(fileURLToPath(import.meta.url)), './path/to/src/locales/**'),
        }),
    ],

    resolve: {
        alias: {// 别名配置,__dirname当前路径,再组合后面的
            '@': path.resolve(__dirname, 'src'),
        }
    },

    server: {
        proxy: {
            '/api': {
                // 要替换的服务器地址,将/api替换为服务器地址
                target: "https://lianghj.top:8888/api/private/v1", // 是否开启代理
                changeOrigin: true, // path为输入地址,将输入地址中,多余的/api(因为是自己加的,非访问对象的)替换为空字符串
                rewrite: (path) => path.replace(/^\/api/, ""),
            },
        },
    },
})
